package org.lilystudio.smarty4j.statement.function;

import java.util.Map;
import org.lilystudio.smarty4j.Context;
import org.lilystudio.smarty4j.expression.IExpression;
import org.lilystudio.smarty4j.statement.Function;
import org.lilystudio.smarty4j.statement.ParameterCharacter;
import org.objectweb.asm.MethodVisitor;

public class $assign2 extends Function {
  private static ParameterCharacter[] definitions = new ParameterCharacter[] { 
		  new ParameterCharacter(3, "var"), 
	      new ParameterCharacter(4, "value"), 
	      new ParameterCharacter(3, null, "delimiter") };
  
  public ParameterCharacter[] getDefinitions() {
    return definitions;
  }
  
  public void parse(MethodVisitor mw, int local, Map<String, Integer> variableNames) {
    IExpression delimiter = getParameter(2);
    mw.visitVarInsn(25, 1);
    IExpression var = getParameter(0);
    var.parse(mw, local, variableNames);
    getParameter(1).parseObject(mw, local, variableNames);
    if (delimiter != null) {
      mw.visitMethodInsn(182, "java/lang/Object", "toString", 
          "()Ljava/lang/String;");
      delimiter.parse(mw, local, variableNames);
      mw.visitMethodInsn(182, "java/lang/String", "split", 
          "(Ljava/lang/String;)[Ljava/lang/String;");
    } 
    Integer index = (variableNames == null) ? null : variableNames.get(var.toString());
    if (index != null) {
      mw.visitInsn(89);
      mw.visitVarInsn(58, index.intValue());
    } 
    mw.visitMethodInsn(182, Context.NAME, "set", 
        "(Ljava/lang/String;Ljava/lang/Object;)V");
  }
}
